package com.hifar.test.api.handler;

import com.hifar.test.api.model.DataResponse;
import com.hifar.test.api.model.FailedData;
import com.hifar.test.api.model.ResponseData;
import com.hifar.test.api.model.ResponseStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 响应处理器
 * 用于处理数据资产管理平台的响应结果
 */
@Component
public class ResponseHandler {
    
    private static final Logger logger = LoggerFactory.getLogger(ResponseHandler.class);
    
    /**
     * 处理响应结果
     * 
     * @param response 响应对象
     * @return 处理结果
     */
    public ProcessResult handleResponse(DataResponse response) {
        if (response == null) {
            logger.error("响应对象为空");
            return ProcessResult.failure("响应对象为空");
        }
        
        // 检查响应状态
        if (ResponseStatus.SUCCESS.getValue().equals(response.getStatus())) {
            return handleSuccessResponse(response);
        } else if (ResponseStatus.FAILURE.getValue().equals(response.getStatus())) {
            return handleFailureResponse(response);
        } else {
            logger.error("未知的响应状态: {}", response.getStatus());
            return ProcessResult.failure("未知的响应状态: " + response.getStatus());
        }
    }
    
    /**
     * 处理成功响应
     * 
     * @param response 响应对象
     * @return 处理结果
     */
    private ProcessResult handleSuccessResponse(DataResponse response) {
        logger.info("请求成功: {}", response.getMessage());
        
        ResponseData data = response.getData();
        if (data != null) {
            Integer successCount = data.getSuccessCount();
            List<FailedData> failedData = data.getFailedData();
            
            if (successCount != null && successCount > 0) {
                logger.info("成功处理 {} 条数据", successCount);
            }
            
            if (failedData != null && !failedData.isEmpty()) {
                logger.warn("存在 {} 条失败数据", failedData.size());
                for (FailedData failed : failedData) {
                    logger.warn("失败数据 - 任务ID: {}, 类型: {}, 原因: {}", 
                            failed.getTaskId(), failed.getType(), failed.getReason());
                }
            }
        }
        
        return ProcessResult.success(response.getMessage());
    }
    
    /**
     * 处理失败响应
     * 
     * @param response 响应对象
     * @return 处理结果
     */
    private ProcessResult handleFailureResponse(DataResponse response) {
        logger.error("请求失败: {}", response.getMessage());
        
        ResponseData data = response.getData();
        if (data != null) {
            List<FailedData> failedData = data.getFailedData();
            if (failedData != null && !failedData.isEmpty()) {
                logger.error("失败数据详情:");
                for (FailedData failed : failedData) {
                    logger.error("失败数据 - 任务ID: {}, 类型: {}, 原因: {}", 
                            failed.getTaskId(), failed.getType(), failed.getReason());
                }
            }
        }
        
        return ProcessResult.failure(response.getMessage());
    }
    
    /**
     * 检查响应是否成功
     * 
     * @param response 响应对象
     * @return 是否成功
     */
    public boolean isSuccess(DataResponse response) {
        return response != null && ResponseStatus.SUCCESS.getValue().equals(response.getStatus());
    }
    
    /**
     * 获取成功数量
     * 
     * @param response 响应对象
     * @return 成功数量
     */
    public int getSuccessCount(DataResponse response) {
        if (response != null && response.getData() != null) {
            Integer successCount = response.getData().getSuccessCount();
            return successCount != null ? successCount : 0;
        }
        return 0;
    }
    
    /**
     * 获取失败数量
     * 
     * @param response 响应对象
     * @return 失败数量
     */
    public int getFailureCount(DataResponse response) {
        if (response != null && response.getData() != null && response.getData().getFailedData() != null) {
            return response.getData().getFailedData().size();
        }
        return 0;
    }
    
    /**
     * 处理结果内部类
     */
    public static class ProcessResult {
        private final boolean success;
        private final String message;
        
        private ProcessResult(boolean success, String message) {
            this.success = success;
            this.message = message;
        }
        
        public static ProcessResult success(String message) {
            return new ProcessResult(true, message);
        }
        
        public static ProcessResult failure(String message) {
            return new ProcessResult(false, message);
        }
        
        public boolean isSuccess() {
            return success;
        }
        
        public String getMessage() {
            return message;
        }
        
        @Override
        public String toString() {
            return "ProcessResult{" +
                    "success=" + success +
                    ", message='" + message + '\'' +
                    '}';
        }
    }
}
